﻿using MSL.Utility.Threading;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Threading
{
    public partial class asyncHelper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = "A";
            switch (type)
            {
                case "A":
                    A();
                    break;
                default: break;
            }
        }
        private void A()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            /* 
            List<string> testFiles = new List<string>(); 
            for (int i = 0; i < 100; i++) 
            { 
                testFiles.Add("D:\\test\\async\\file_" + i.ToString() + ".log"); 
            } 
            AsyncHelper.DoAsync(testFiles, 10, WriteFile); 
  
            Console.WriteLine("异步写耗时"+sw.ElapsedMilliseconds + "ms"); 
            */

            List<string> list = new List<string>();
            for (int i = 0; i < 200; i++)
            {
                list.Add("D:\\test\\async\\file_" + i.ToString() + ".log");
            }

            Hashtable result;
            AsyncHelper.DoAsync(list, 20, WriteFileAndReturnRowCount, false, out result);

            Response.Write("异步写耗时" + sw.ElapsedMilliseconds + "ms");

            Thread.Sleep(10);

            if (result != null)
            {
                foreach (object key in result.Keys)
                {
                    Console.WriteLine("{0}={1}", key, result[key]);
                }
            }
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 200; i++)
            {
                WriteFile("D:\\test\\sync\\file_" + i.ToString() + ".log");
            }

            Response.Write("同步写耗时" + sw.ElapsedMilliseconds + "ms");
        }

        void WriteFile(object objFilePath)
        {
            string filePath = (string)objFilePath;
            string dir = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            int rowCn = 10000;
            using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.Default))
            {
                for (int i = 0; i < rowCn; i++) writer.WriteLine(Guid.NewGuid());
            }
        }

        object WriteFileAndReturnRowCount(object objFilePath)
        {
            string filePath = (string)objFilePath;
            string dir = Path.GetDirectoryName(filePath);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            int rowCn = 10000;
            using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.Default))
            {
                for (int i = 0; i < rowCn; i++) writer.WriteLine(Guid.NewGuid());
            }
            return DateTime.Now.ToLongTimeString();
        }
    }
}